cc.Class({
    extends: cc.Component,
    properties: {
        blurMaterial: {
            type: cc.Material,
            default: null,
            displayName: "模糊材质",
            tooltip: "用于应用模糊所用的材质，如无特殊需求请保持默认",
            default: null
        },
        ignoredNodes: {
            type: cc.Node,
            default: [],
            displayName: "忽略节点列表",
            tooltip: "在此列表内容的节点不会被渲染到模糊贴图里",
        },
        _bightness: {
            type: cc.Float,
            default: 0.5,
            min: 0,
            max: 1,
        },
        bightness: {
            get() {
                return this._bightness;
            },
            set(val) {
                this._bightness = val;
                if (this.blurMaterial) {
                    this.blurMaterial.setProperty( "bightness", this._bightness );
                    this.materialDirty = true;
                }
            }
        },
        _blurAmount: {
            type: cc.Float,
            default: 0.5,
            min: 0,
            max: 1
        },
        blurAmount: {
            get() {
                return this._blurAmount;
            },
            set(val) {
                this._blurAmount = val;
                if (this.blurMaterial) {
                    this.blurMaterial.setProperty( "blurAmount", this._blurAmount );
                    this.materialDirty = true;
                }
            }
        },
        _lastSize : cc.Size,
        _cullingMask : 0x10000000,
        maskDirty : {
            type : cc.Boolean,
            default : false,
            visible : false,
        },
        materialDirty : {
            type : cc.Boolean,
            default : false,
            visible : false,
        }
    },
    start() {
        this.node.scaleY = -1;

        this.texture = new cc.RenderTexture();
        
        //有MASK，要加上STENCIL_INDEX8;
        this.texture.initWithSize(this.node.width, this.node.height, cc.game._renderContext.STENCIL_INDEX8);
        // this.texture.initWithSize(this.node.width, this.node.height);

        this.camera = this.node.addComponent(cc.Camera);
        this.camera.cullingMask = 0xffffffff ^ this._cullingMask;
        this.camera.targetTexture = this.texture;

        //只渲染到贴图中。
        this.camera.enabled = false;

        this.cull(this.node);

        this.ignoredNodes.map(function (node) {
            this.cull(node);
        }.bind(this)); 

        //创建一个sprite，显示
        this.spriteFrame = new cc.SpriteFrame();
        this.sprite = this.node.addComponent(cc.Sprite);
        this.sprite.spriteFrame = this.spriteFrame;

        this.bightness = this._bightness;
        this.blurAmount = this._blurAmount;

        this.sprite["_materials"][0] = this.blurMaterial;
    },
    snapShot(){
        if( this.materialDirty ) {
            this.sprite.setMaterial(0, this.blurMaterial );
            this.materialDirty = false;
        }

        let size = this.node.getContentSize();
        if( size.width !== this._lastSize.width || size.height !== this._lastSize.height ) {
            this.texture.initWithSize( this.node.width, this.node.height, cc.game._renderContext.STENCIL_INDEX8);
            this.camera.targetTexture = this.texture;
        }
        this._lastSize.width = size.width;
        this._lastSize.height = size.height;

        // 手动渲染摄影机，保存截图
        // cc.Canvas.instance.node
		// 应用刚刚截图的贴图到sprite身上进行渲染
        this.camera.render( cc.Canvas.instance.node );
        this.spriteFrame.setTexture( this.texture );

        this.maskDirty = false;
    },
    update( dt ){
        // if( this.maskDirty )
        {
            this.snapShot();
        }
    },
    cull(node) {
        if (node) {
            node["_cullingMask"] = this._cullingMask;
            if (node.childrenCount > 0) {
                node.children.map(child => this.cull(child));
            }
        }
    }
});
